//
//  HandleCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/7.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class HandleCell: UITableViewCell {

    let titleImageView = UIImageView() //图片
    let titleLabel = UILabel() //标题
    let newLabel = UILabel() //内容
    let numberLabel = UILabel() //申请人数
    let requestBtn = UIButton() //申请
    
    fileprivate let cashLabel = UILabel() //取现
    fileprivate let freeLabel = UILabel() //免息期
    fileprivate let yearLabel = UILabel() //免年费
    
    var model : HandleModel!{
        
        didSet{
            updateData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.white
        
        self.contentView.addSubview(titleImageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(titleLabel)
        
        newLabel.textColor = UIColor.lightGray
        newLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(newLabel)
        
        let alreadyLabel = UILabel()
        alreadyLabel.text = already
        alreadyLabel.textColor = UIColor.lightGray
        alreadyLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(alreadyLabel)
        
        numberLabel.textColor = UIColor.red
        numberLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(numberLabel)
        
        let personLabel = UILabel()
        personLabel.text = peopleForApplication
        personLabel.textColor = UIColor.lightGray
        personLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.contentView.addSubview(personLabel)
        
        requestBtn.setTitle(freeApplication, for: .normal)
        requestBtn.backgroundColor = UIColor.red
        requestBtn.setTitleColor(UIColor.white, for: .normal)
        requestBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        requestBtn.layer.cornerRadius = 5
        requestBtn.layer.masksToBounds = true
        self.contentView.addSubview(requestBtn)
        
        cashLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        cashLabel.textAlignment = .center
        cashLabel.layer.borderColor = UIColor.lineColor().cgColor
        cashLabel.layer.borderWidth = 1
        cashLabel.layer.cornerRadius = 5
        cashLabel.layer.masksToBounds = true
        self.contentView.addSubview(cashLabel)
        
        freeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        freeLabel.textAlignment = .center
        freeLabel.layer.borderColor = UIColor.lineColor().cgColor
        freeLabel.layer.borderWidth = 1
        freeLabel.layer.cornerRadius = 5
        freeLabel.layer.masksToBounds = true
        self.contentView.addSubview(freeLabel)
        
        yearLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        yearLabel.textAlignment = .center
        yearLabel.layer.borderColor = UIColor.lineColor().cgColor
        yearLabel.layer.borderWidth = 1
        yearLabel.layer.cornerRadius = 5
        yearLabel.layer.masksToBounds = true
        self.contentView.addSubview(yearLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        titleImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 75))
            make.width.equalTo(RATIO(maxNum: 120))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleImageView.snp.right).offset(RATIO(maxNum: 8))
            make.right.equalToSuperview().offset(RATIO(maxNum: -8))
            make.top.equalTo(titleImageView)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        newLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.right.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(titleLabel)
        }
        
        alreadyLabel.snp.makeConstraints { (make) in
            make.left.equalTo(newLabel)
            make.width.greaterThanOrEqualTo(10)
        make.top.equalTo(newLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(newLabel)
        }
        
        numberLabel.snp.makeConstraints { (make) in
            make.left.equalTo(alreadyLabel.snp.right)
            make.width.greaterThanOrEqualTo(10)
            make.centerY.equalTo(alreadyLabel)
            make.height.equalTo(alreadyLabel)
        }
        
        personLabel.snp.makeConstraints { (make) in
            make.left.equalTo(numberLabel.snp.right)
            make.centerY.equalTo(numberLabel)
            make.height.equalTo(numberLabel)
            make.width.greaterThanOrEqualTo(10)
        }
        
        requestBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -8))
            make.top.equalTo(titleImageView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 80))
            make.height.equalTo(RATIO_H(maxNum: 25))
        }
        
        yearLabel.snp.makeConstraints { (make) in
        make.right.equalTo(requestBtn.snp.left).offset(RATIO(maxNum: RATIO(maxNum: -8)))
            make.centerY.height.equalTo(requestBtn)
        make.left.equalTo(self.contentView.snp.centerX).offset(RATIO(maxNum: 4))
        }

        cashLabel.snp.makeConstraints { (make) in
            make.centerY.height.equalTo(yearLabel)
            make.left.equalTo(titleImageView)
            make.width.equalTo(requestBtn)
        }
        
        freeLabel.snp.makeConstraints { (make) in
            make.centerY.height.equalTo(cashLabel)
            make.left.equalTo(cashLabel.snp.right).offset(RATIO(maxNum: 8))
            make.right.equalTo(self.contentView.snp.centerX).offset(RATIO(maxNum: -4))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    //MARK: 刷新数据
    fileprivate func updateData() {
        
        titleImageView.setUrlImage(with: kmHttps + model.image!, placeholder: UIImage.imageWithName("loading"))
        titleLabel.text = model.credit_name!
        newLabel.text = model.favorable
        numberLabel.text = model.num
        cashLabel.text = withdrawCash + model.card_edu!
        freeLabel.text = freePeriod + model.card_mxq!
        yearLabel.text = model.card_nfzc
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }

}
